package br.com.etecmatao.admin.controller;

import br.com.etecmatao.business.BusinessLocator;
import br.com.etecmatao.business.impl.BusinessLocatorImpl;
import br.com.etecmatao.model.Curso;
import br.com.etecmatao.model.Download;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

@ManagedBean
public class FormDownload {

    private Download download;
    private BusinessLocator businessLocator;
    private DataModel<Download> lista;

    public DataModel<Download> getLista() {
        lista = new ListDataModel<Download>(businessLocator.getDowloadBusiness().finByAll());
        return lista;
    }

    public void setLista(DataModel<Download> lista) {
        this.lista = lista;
    }

    public FormDownload() {
        download = new Download();
        businessLocator = new BusinessLocatorImpl();
    }

    public Download getDownload() {
        return download;
    }

    public void setDownload(Download download) {
        this.download = download;
    }
    public void save(){
       download.setStatus("A");
        if (download.getIdDownload() != null) {
            businessLocator.getDowloadBusiness().update(download);
        }else{
            businessLocator.getDowloadBusiness().save(download);
        }
       FacesContext.getCurrentInstance().addMessage("messages", new FacesMessage("Cadastro realizado com sucesso"));

    }

    public void delete(){
        download = lista.getRowData();
        businessLocator.getDowloadBusiness().delete(download);
    }

    public String editar(){
        download = lista.getRowData();
        return "formDownload";
    }

    public String create(){
        download = new Download();
        return "formDownload";
    }
}
